package com.huishu.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huishu.commons.pojo.BasePage;
import com.huishu.commons.pojo.Result;
import com.huishu.user.mapper.MemberStyleMapper;
import com.huishu.user.pojo.model.MemberStyleDO;
import com.huishu.user.service.MemberStyleService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator@ljj
 * @since 2023-06-28
 */
@Service
public class MemberStyleServiceImpl extends ServiceImpl<MemberStyleMapper, MemberStyleDO> implements MemberStyleService {

    @Override
    public Result pageList(BasePage basePage) {
        Page<MemberStyleDO> page = new Page<>(basePage.getCurrPage(), basePage.getPageSize());
        LambdaQueryWrapper<MemberStyleDO> queryWrapper = Wrappers.lambdaQuery(MemberStyleDO.class)
                .orderByDesc(MemberStyleDO::getCreatedDate);
        return Result.success(this.baseMapper.selectPage(page, queryWrapper));
    }

    @Override
    public Result openOrClose(MemberStyleDO memberStyleDO) {
        if (memberStyleDO.getIsOpen()) {
            // 打开
            LambdaQueryWrapper<MemberStyleDO> queryWrapper = Wrappers.lambdaQuery(MemberStyleDO.class)
                    .eq(MemberStyleDO::getIsOpen, true)
                    .ne(MemberStyleDO::getId, memberStyleDO.getId());
            MemberStyleDO one = this.getOne(queryWrapper);
            if (one != null) {
                return Result.fail("只能存在一个打开着的样式!!");
            }
        }
        LambdaUpdateWrapper<MemberStyleDO> uw = Wrappers.lambdaUpdate(MemberStyleDO.class)
                .set(MemberStyleDO::getIsOpen, memberStyleDO.getIsOpen())
                .eq(MemberStyleDO::getId, memberStyleDO.getId());
        return this.update(uw) ? Result.success() : Result.fail();
    }
}
